/* eslint-disable no-unused-vars */
import BaseRenderer from 'diagram-js/lib/draw/BaseRenderer';
import { getRoundRectPath } from 'bpmn-js/lib/draw/BpmnRenderUtil';
import { attr as svgAttr } from 'tiny-svg';

import { HIGH_PRIORITY, TASK_BORDER_RADIUS } from '../config';
import { getExtension } from './utility';
import BlankNode from './blank-node';
import ConfirmInfoNode from './confirm-info-node';
import speakWaitingNode from './speak-waiting-node';
import detectLivingFaceNode from './detect-living-face-node';
import messageCollectNode from './message-collect-node';
import signContractNode from './sign-contract-node';
import faceCompareNode from './face-compare-node';
import sendMessageNode from './send-message-node';
import requestLocationNode from './request-location-node';
import transferNode from './transfer-node';
import scanCodeNode from './scan-code-node';
import hangupNode from './hangup-node';
import businessNavigationNode from './business-navigation-node';
import idCardCompareNode from './id-card-compare-node';
import sendFormNode from './send-form-node';
import readWaitNode from './waiting-read-node';
import sceneTransferNode from './scene-transfer-node';
import summaryConfirmNode from './summary-confirm-node';
import silentWatingNode from './silent-wating-node';
import optionJudgeNode from './option-judge-node';
import messageSummary from './message-summary-node';
import sendContentsNode from './send-contents-node';
import soundReceptionNode from './sound-reception-node';
import nlpNode from './nlp-node';
import variableJudgeNode from './variable-judge-node';
import sendDepositionNode from './send-deposition-node';
import sendSignNode from './send-sign-node';
import variableCollection from './variable-collection-node';
import inviteHumanThreeParties from './invite-human-three-parties-node';
import collectShortVideo from './video-collect-node';
import interfaceCall from './interface-call-node';
import startSystemRecordingNode from './start-recording-node';
import stopSystemRecordingNode from './stop-recording-node';
import cardCollectionNode from './card-collection-node';
import llmNode from './llm-node';
import { getAllParentNodes, validateSummary } from '../../../../utility/utility';
import AIWorkflow from '@/modules/workFlow.js';

// import store from '../../../../store'
// import {
//     query as domQuery
//   } from 'min-dom';
const _ = require('lodash');
// const filterDeep = require('deepdash/getFilterDeep')(_);

/**
 * A renderer that knows how to render custom elements.
 */
export default class CustomRenderer extends BaseRenderer {
	constructor(eventBus, bpmnRenderer, modeling, textRenderer, moddle, elementRegistry, elementFactory, injector, bpmnFactory, styles, selection) {
		super(eventBus, HIGH_PRIORITY);

		this.bpmnRenderer = bpmnRenderer;
		this.modeling = modeling;
		this.textRenderer = textRenderer;
		this.elementRegistry = elementRegistry;
		this.moddle = moddle;
		this.eventBus = eventBus;
		this.autoPlace = injector.get('autoPlace', false);
		window.isReplaceMode = false;

		console.log('elementRegistry : ', elementRegistry, styles);

		const getBlankNodeElements = function () {
			let elements = elementRegistry.filter(function (element) {
				return element.type === 'bpmn:Task';
			});
			return elements;
		};
		const getElementByTaskId = function (taskId) {
			let elements = elementRegistry.filter(function (element) {
				let id = element.businessObject ? element.businessObject.taskId : element.id;
				return id === taskId;
			});
			return elements && elements.length > 0 ? elements[0] : null;
		};
		const getStartNode = function () {
			const startEvent = elementRegistry.get('StartEvent_1');
			if (startEvent && startEvent.outgoing && startEvent.outgoing.length > 0) {
				const startNode = elementRegistry.get(startEvent.outgoing[0].id).target;
				// console.log('getStartNode : ', startNode);
				return startNode.businessObject.taskId;
			}
			return '';
		};

		const updateTaskElements = function () {
			let taskElements = elementRegistry.filter(function (element) {
				return (
					element.type === 'bpmn:Task' &&
					!element.businessObject.taskId.includes('BlankNode') &&
					!element.businessObject.taskId.includes('transfer') &&
					!element.businessObject.taskId.includes('hangup')
				);
			});
			eventBus.fire('elements.changed', {
				elements: taskElements
			});
		};

		// 监听store变化，存储工作流数据
		const unsubscribe = window.store.subscribe((mutation, state) => {
			if (mutation.type === 'workflow/workflow/SET_UPDATED_NEXT_STEPS') {
				console.log('###subscribe : ', mutation, state);
				const taskId = mutation.payload.taskId;
				const steps = mutation.payload.steps;
				const curElement = getElementByTaskId(taskId);
				if (!curElement) {
					return;
				}
				const blankNodes = steps.length;
				const x0 = curElement.x;
				const y0 = curElement.y;
				const width0 = curElement.width;
				const autoPlace = injector.get('autoPlace', false);
				for (let i = 0; i < blankNodes; i++) {
					// delete exist connection
					if (steps[i].previous && steps[i].previous.length > 0) {
						const previousNode = getElementByTaskId(steps[i].previous);
						if (previousNode) {
							const incoming = previousNode.businessObject.incoming;
							console.log(incoming);
							if (incoming && incoming.length > 0) {
								incoming.map(item => {
									const connect = elementRegistry.get(item.id);
									console.log('delete exist connection : ', connect.businessObject.name);
									if (connect && curElement.id === connect.source.id && connect.businessObject.name === steps[i].text) {
										console.log(connect, steps[i].text);
										console.log(modeling);
										setTimeout(() => {
											modeling.removeElements([connect]);
										});
									}
								});
							}
						}
					}

					// create blank node
					if ((!steps[i].current || steps[i].current.length <= 0) && steps[i].previous) {
						const nextNode = getElementByTaskId(steps[i].previous);
						// 删除连线
						const outgoing = curElement.businessObject.outgoing;
						if (nextNode && outgoing && outgoing.length > 0) {
							outgoing.map(item => {
								const connect = elementRegistry.get(item.id);
								console.log('item : ', item.targetRef, nextNode);
								if (item.targetRef.id === nextNode.businessObject.id && connect.businessObject.name === steps[i].text) {
									modeling.removeElements([connect]);
								}
							});
						}
					} else if (steps[i].current === 'BlankNode') {
						const businessObject = bpmnFactory.create('bpmn:Task');
						businessObject['taskId'] = 'BlankNode' + '_' + window.aiWorkFlowlId++;
						console.log('create blank node : ', businessObject['taskId']);
						let newShape = null;
						try {
							const shape = elementFactory.createShape({
								type: 'bpmn:Task',
								id: businessObject['taskId'],
								businessObject
							});
							newShape = autoPlace.append(curElement, shape);
						} catch (error) {
							console.error('create shape error : ', error);
							continue;
						}
						const x1 = newShape.x;
						const y1 = newShape.y;
						const width1 = newShape.width;
						const height = newShape.height;
						const span = (width0 - width1) / 2;
						if (blankNodes === 1) {
							modeling.moveElements([newShape], { x: -(x1 - x0 - span), y: x1 - x0 - width0 + height });
						} else if (blankNodes === 2) {
							if (i === 0) {
								modeling.moveElements([newShape], { x: -(x1 - x0 + width1), y: x1 - x0 - width0 + height });
							} else {
								modeling.moveElements([newShape], { x: -(x1 - x0 - width0), y: x1 - x0 - width0 + height });
							}
						} else if (blankNodes === 3) {
							if (i === 0) {
								modeling.moveElements([newShape], { x: -(x1 - x0 + width0 + 20), y: x1 - x0 - width0 + height });
							} else if (i === 1) {
								modeling.moveElements([newShape], { x: -(x1 - x0 - span), y: x1 - x0 - width0 + height });
							} else {
								modeling.moveElements([newShape], { x: -(x1 - x0 - width0 - 20 - (width0 - width1)), y: x1 - x0 - width0 + height });
							}
						} else if (blankNodes === 4) {
							if (i === 0) {
								modeling.moveElements([newShape], { x: -(x1 - x0 + width0 * 1.5 + 30 - span), y: x1 - x0 - width0 + height });
							} else if (i === 1) {
								modeling.moveElements([newShape], { x: -(x1 - x0 + width0 / 2 + 10 - span), y: x1 - x0 - width0 + height });
							} else if (i === 2) {
								modeling.moveElements([newShape], { x: -(x1 - x0 - width0 / 2 - 10 - span), y: x1 - x0 - width0 + height });
							} else {
								modeling.moveElements([newShape], { x: -(x1 - x0 - width0 * 1.5 - 30 - span), y: x1 - x0 - width0 + height });
							}
						} else if (blankNodes === 5) {
							if (i === 0) {
								modeling.moveElements([newShape], { x: -(x1 - x0 - span + width0 * 2 + 60), y: x1 - x0 - width0 + height });
							} else if (i === 1) {
								modeling.moveElements([newShape], { x: -(x1 - x0 - span + width0 + 30), y: x1 - x0 - width0 + height });
							} else if (i === 2) {
								modeling.moveElements([newShape], { x: -(x1 - x0 - span), y: x1 - x0 - width0 + height });
							} else if (i === 3) {
								modeling.moveElements([newShape], { x: -(x1 - x0 - width0 - 30 - span), y: x1 - x0 - width0 + height });
							} else {
								modeling.moveElements([newShape], { x: -(x1 - x0 - width0 * 2 - 60 - span), y: x1 - x0 - width0 + height });
							}
						} else if (blankNodes === 6) {
							console.log('####element point', x0, x1, width0, width1, y1, height);
							if (i === 0) {
								modeling.moveElements([newShape], { x: -(x1 - x0 - span + width0 / 2 + (span + width0) * 2), y: x1 - x0 - width0 + height });
							} else if (i === 1) {
								modeling.moveElements([newShape], { x: -(x1 - x0 - span + width0 / 2 + span + width0), y: x1 - x0 - width0 + height });
							} else if (i === 2) {
								modeling.moveElements([newShape], { x: -(x1 - x0 - span + width0 / 2), y: x1 - x0 - width0 + height });
							} else if (i === 3) {
								modeling.moveElements([newShape], { x: -(x1 - x0 - span - width0 / 2), y: x1 - x0 - width0 + height });
							} else if (i === 4) {
								modeling.moveElements([newShape], { x: -(x1 - x0 - span - width0 / 2 - span - width0), y: x1 - x0 - width0 + height });
							} else {
								modeling.moveElements([newShape], { x: -(x1 - x0 - span - width0 / 2 - (span + width0) * 2), y: x1 - x0 - width0 + height - 100 });
							}
						} else if (blankNodes === 7) {
							console.log('####element point', x0, x1, width0, width1, y1, height);
							if (i === 0) {
								modeling.moveElements([newShape], { x: -(x1 - x0 - span + width0 * 3 + 90), y: x1 - x0 - width0 + height });
							} else if (i === 1) {
								modeling.moveElements([newShape], { x: -(x1 - x0 - span + width0 * 2 + 60), y: x1 - x0 - width0 + height });
							} else if (i === 2) {
								modeling.moveElements([newShape], { x: -(x1 - x0 - span + width0 + 30), y: x1 - x0 - width0 + height });
							} else if (i === 3) {
								modeling.moveElements([newShape], { x: -(x1 - x0 - span), y: x1 - x0 - width0 + height });
							} else if (i === 4) {
								modeling.moveElements([newShape], { x: -(x1 - x0 - width0 - 30 - span), y: x1 - x0 - width0 + height });
							} else if (i === 5) {
								modeling.moveElements([newShape], { x: -(x1 - x0 - width0 * 2 - 60 - span), y: x1 - x0 - width0 + height });
							} else {
								modeling.moveElements([newShape], { x: -(x1 - x0 - width0 * 3 - 90 - span), y: x1 - x0 - width0 + height });
							}
						}
						let flowConnection = elementRegistry.get(newShape.businessObject.incoming[0].id);
						modeling.updateProperties(flowConnection, { name: steps[i].text });
						// 先添加新节点
						const data = { no: newShape.businessObject.taskId, completed: false, id: newShape.businessObject.taskId };
						window.store.dispatch('workflow/addWorkStep', data);
						console.log({ parentId: mutation.payload.taskId, type: steps[i].type, no: newShape.businessObject.taskId, data: data });
						window.store.dispatch('workflow/updateNodeId', { parentId: mutation.payload.taskId, type: steps[i].type, no: newShape.businessObject.taskId });
					} else {
						// connect two element -> createConnection(source, target, attrs, source.parent, hints);
						const nextNode = getElementByTaskId(steps[i].current);
						if (!nextNode) {
							console.error('nextNode is null : ', steps[i].current, elementRegistry);
						}
						// check whether connected
						// 不删除已有节点
						// const outgoing = curElement.businessObject.outgoing
						// if (outgoing && outgoing.length > 0) {
						//     outgoing.map(item => {
						//         // console.log('item : ', item.targetRef, nextNode);
						//         if (item.targetRef.id === nextNode.businessObject.id) {
						//             const connect = elementRegistry.get(item.id)
						//             modeling.removeElements([connect]);
						//         }
						//     })
						// }
						const businessObject = bpmnFactory.create('bpmn:SequenceFlow', {
							name: steps[i].text
						});
						modeling.createConnection(
							curElement,
							nextNode,
							{
								type: 'bpmn:SequenceFlow',
								businessObject: businessObject
							},
							curElement.parent
						);
					}
				}
				// updateTaskValidate(taskId, steps, state.workflow.worksteps);
				updateTaskElements();
				// 更改下一步连线，校验信息汇总模板，节点内容推送汇总项下拉框
				// todo 引起别的问题 ，比如 http://192.168.31.91:7080/browse/IVCS-2334
				// const worksteps = validateSummary(state.workflow.worksteps, taskId, state.workflow.startNode);
				// window.store.dispatch('workflow/setWorkSteps', worksteps);
				setTimeout(() => {
					// element.dispatchEvent(event);
					selection.select(curElement);
				}, 0);
			} else if (mutation.type === 'workflow/workflow/ADD_WORK_STEP') {
				// add workflow step to businessObject

				let elements = elementRegistry.filter(function (element) {
					let taskId = element.businessObject ? element.businessObject.taskId : null;
					taskId = taskId || element.id;
					return taskId && taskId.includes(mutation.payload.id);
				});
				if (elements && elements.length > 0) {
					const businessObject = elements[0].businessObject || {};
					businessObject.taskId = mutation.payload.id;
					let steps = getExtension(businessObject, 'ai:steps');
					if (!steps) {
						businessObject.extensionElements = businessObject.extensionElements || moddle.create('bpmn:ExtensionElements');
						steps = moddle.create('ai:steps');
						businessObject.extensionElements.get('values').push(steps);
					}
					steps.data = JSON.stringify(mutation.payload);
					// update the label of an element
					modeling.updateLabel(elements[0], '');
				}
				updateTaskElements();
				window.isReplaceMode = false;
				// add new blank node command:jump-to
				// if (elements[0] && elements[0].outgoing && elements[0].outgoing.length > 0) {
				//     this.updateElementConnect(elements[0],mutation.payload);
				//     return;
				// }
			} else if (mutation.type === 'workflow/workflow/REMOVE_WORK_STEP') {
				updateTaskElements();
			} else if (mutation.type === 'workflow/workflow/HIDE_SPEAK_WAIT') {
				let elements = elementRegistry.filter(function (element, index) {
					console.log('workflow/workflow/HIDE_SPEAK_WAIT ', element, index);
					let taskId = element.businessObject ? element.businessObject.taskId : null;
					taskId = taskId || element.id;
					return taskId;
				});
			} else if (mutation.type === 'workflow/workflow/UPDATE_BREAK_RULE_STATUS') {
				updateTaskElements();
			} else if (mutation.type === 'workflow/workflow/VERIFY_INVALID_ARIABLE') {
				updateTaskElements();
			}
		});
		// you may call unsubscribe to stop the subscription
		// unsubscribe()
		// 双击打开编辑器
		// eventBus.on('element.dblclick', 1500, function (event) {
		// 	let element = event.element;
		// 	console.log('element.dblclick : ', element);
		// 	if (window.store.state.workflow.isViewFlow) {
		// 		return;
		// 	}

		// 	if (element && element.type === 'bpmn:Task') {
		// 		const taskId = element.businessObject.taskId || element.id;

		// 		if (
		// 			taskId.includes(ConfirmInfoNode.name) ||
		// 			taskId.includes(speakWaitingNode.name) ||
		// 			taskId.includes(detectLivingFaceNode.name) ||
		// 			taskId.includes(messageCollectNode.name) ||
		// 			taskId.includes(signContractNode.name) ||
		// 			taskId.includes(faceCompareNode.name) ||
		// 			taskId.includes(sendMessageNode.name) ||
		// 			taskId.includes(requestLocationNode.name) ||
		// 			taskId.includes(transferNode.name) ||
		// 			taskId.includes(scanCodeNode.name) ||
		// 			taskId.includes(hangupNode.name) ||
		// 			taskId.includes(businessNavigationNode.name) ||
		// 			taskId.includes(idCardCompareNode.name) ||
		// 			taskId.includes(sendFormNode.name) ||
		// 			taskId.includes(readWaitNode.name) ||
		// 			taskId.includes(sceneTransferNode.name) ||
		// 			taskId.includes(summaryConfirmNode.name) ||
		// 			taskId.includes(silentWatingNode.name) ||
		// 			taskId.includes(optionJudgeNode.name) ||
		// 			taskId.includes(messageSummary.name) ||
		// 			taskId.includes(sendContentsNode.name) ||
		// 			taskId.includes(nlpNode.name) ||
		// 			taskId.includes(soundReceptionNode.name) ||
		// 			taskId.includes(variableJudgeNode.name) ||
		// 			taskId.includes(sendDepositionNode.name) ||
		// 			taskId.includes(sendSignNode.name) ||
		// 			taskId.includes(variableCollection.name) ||
		// 			taskId.includes(inviteHumanThreeParties.name) ||
		// 			taskId.includes(collectShortVideo.name) ||
		// 			taskId.includes(interfaceCall.name) ||
		// 			taskId.includes(startSystemRecordingNode.name) ||
		// 			taskId.includes(stopSystemRecordingNode.name) ||
		// 			taskId.includes(cardCollectionNode.name)
		// 		) {
		// 			window.store.dispatch('workflow/setCurShowNode', taskId);
		// 		}
		// 	}
		// 	event.stopPropagation();
		// });

		eventBus.on('create.rejected', function (event) {
			// 是否已经配置全局AI属性
			if (AIWorkflow.notifyConfRobotProperty()) {
				return;
			}
			if (!event.hover || !event.hover.businessObject || !event.hover.businessObject.taskId) {
				return;
			}
			if (event.hover.businessObject.taskId.includes(BlankNode.name)) {
				//是否是一个有incoming的空节点
				if (!event.hover.businessObject.incoming) {
					return;
				}
				window.isReplaceMode = true;
				const x0 = event.hover.x;
				const y0 = event.hover.y;
				const width0 = event.hover.width;
				let incomings = event.hover.businessObject.incoming;
				let sourceData = {};
				let source = null;
				let sources = [];
				let blankNodeId = '';
				// 遍历父节点, 获取与每个父节点连线的标注
				incomings.map((incoming, index) => {
					const sourceId = incoming.id;
					const connect = elementRegistry.get(sourceId);
					source = connect.source;
					const connectName = connect.businessObject.name;
					sourceData[source.businessObject.taskId + index] = { connectName: connectName };
					sources.push(source);
				});
				// 删除空白节点
				const blankNode = elementRegistry.get(event.hover.id);
				if (blankNode) {
					// remove blank node
					blankNodeId = blankNode.businessObject.taskId;
					modeling.removeElements([blankNode]);
				}
				// 添加新节点
				const businessObject = bpmnFactory.create('bpmn:Task');
				businessObject['taskId'] = event.shape.businessObject['taskId'];
				const shape = elementFactory.createShape({
					type: 'bpmn:Task',
					id: businessObject['taskId'],
					businessObject
				});
				const autoPlace = injector.get('autoPlace', false);
				let newShape = autoPlace.append(sources[0], shape);
				const x1 = newShape.x;
				const y1 = newShape.y;
				const width1 = newShape.width;
				modeling.moveElements([newShape], { x: x0 - x1 - (width1 - width0) / 2, y: y0 - y1 });
				// 添加其他连线
				if (sources.length > 1) {
					sources.map((target, index) => {
						if (index > 0) {
							modeling.connect(target, shape);
						}
					});
				}
				// store中添加新节点
				const data = { no: newShape.businessObject.taskId, completed: false, id: newShape.businessObject.taskId };

				console.log(data);
				AIWorkflow.addTalk(data);
				window.store.dispatch('workflow/addWorkStep', data);
				// 新连线添加标注
				incomings = newShape.businessObject.incoming;
				incomings.map((incoming, index) => {
					const sourceId = incoming.id;
					const connect = elementRegistry.get(sourceId);
					source = connect.source;
					const taskId = source.businessObject.taskId;
					const connectName = sourceData[taskId + index] ? sourceData[taskId + index].connectName : '';
					modeling.updateProperties(connect, { name: connectName });
					// store中替换新节点
					// {parentId, oldId, newId}
					window.store.dispatch('workflow/relaceNodeId', { parentId: taskId, oldId: blankNodeId, newId: newShape.businessObject.taskId });
					// update source's shape
					modeling.updateLabel(source, '');
				});
				// // store中删除空白节点
				setTimeout(() => {
					window.store.dispatch('workflow/removeWorkStep', blankNodeId);
					window.store.dispatch('workflow/setCurShowNode', newShape.businessObject.taskId);
				}, 0);
				//
				// window.store.dispatch('workflow/setCurShowNode', newShape.businessObject.taskId);
			}
		});
		// shape.remove
		eventBus.on('shape.remove', function (event) {
			if (event.element.type === 'bpmn:Task' && !window.isReplaceMode) {
				// console.log('shape.remove : ', event)
				AIWorkflow.removeTalk(event.element.businessObject.taskId);
				window.store.dispatch('workflow/removeWorkStep', event.element.businessObject.taskId);
				window.store.dispatch('workflow/resetCurShowNode');
			}
		});
		// element.changed
		eventBus.on('element.changed', function (event) {
			// update start node
			const startNode = getStartNode();
			AIWorkflow.setStartNode(startNode);
			window.store.dispatch('workflow/setStartNode', startNode);
		});
		// diagram.destroy
		eventBus.on('diagram.destroy', function (event) {
			const elements = getBlankNodeElements() || [];
			modeling.removeElements(elements);
		});
		// diagram error
		eventBus.on('error', function (event) {
			console.error('diagram error : ', event);
			return false;
		});
		eventBus.on('element.click', event => {
			if (window.store.state.workflow.isViewFlow) {
				return;
			}
			let element = event.element;

			if (element && element.type === 'bpmn:Task') {
				setTimeout(() => {
					selection.select(element);
				}, 0);
				const taskId = element.businessObject.taskId || element.id;

				if (window.store.state.workflow.taskId === taskId) {
					// console.log('event.gfx', event.gfx);
					return;
				}

				if (
					taskId.includes(ConfirmInfoNode.name) ||
					taskId.includes(speakWaitingNode.name) ||
					taskId.includes(detectLivingFaceNode.name) ||
					taskId.includes(messageCollectNode.name) ||
					taskId.includes(signContractNode.name) ||
					taskId.includes(faceCompareNode.name) ||
					taskId.includes(sendMessageNode.name) ||
					taskId.includes(requestLocationNode.name) ||
					taskId.includes(transferNode.name) ||
					taskId.includes(scanCodeNode.name) ||
					taskId.includes(hangupNode.name) ||
					taskId.includes(businessNavigationNode.name) ||
					taskId.includes(idCardCompareNode.name) ||
					taskId.includes(sendFormNode.name) ||
					taskId.includes(readWaitNode.name) ||
					taskId.includes(sceneTransferNode.name) ||
					taskId.includes(summaryConfirmNode.name) ||
					taskId.includes(silentWatingNode.name) ||
					taskId.includes(optionJudgeNode.name) ||
					taskId.includes(messageSummary.name) ||
					taskId.includes(sendContentsNode.name) ||
					taskId.includes(nlpNode.name) ||
					taskId.includes(soundReceptionNode.name) ||
					taskId.includes(variableJudgeNode.name) ||
					taskId.includes(sendDepositionNode.name) ||
					taskId.includes(sendSignNode.name) ||
					taskId.includes(variableCollection.name) ||
					taskId.includes(inviteHumanThreeParties.name) ||
					taskId.includes(collectShortVideo.name) ||
					taskId.includes(interfaceCall.name) ||
					taskId.includes(startSystemRecordingNode.name) ||
					taskId.includes(stopSystemRecordingNode.name) ||
					taskId.includes(cardCollectionNode.name) ||
					taskId.includes(llmNode.name)
				) {
					window.store.dispatch('workflow/setCurShowNode', taskId);
				} else {
					window.store.dispatch('workflow/resetCurShowNode');
				}
			} else {
				window.store.dispatch('workflow/resetCurShowNode');
			}
			event.stopPropagation();
		});
	}

	/**
	 * 更新节点的连线《用于节点编辑后,重新设置连线》
	 * @param {element} element 当前编辑的节点对象
	 * @param {object} workflow 当前节点的流程内容
	 */
	updateElementConnect(element, workflow) {
		console.log('updateElementConnect', element, workflow);
		const workflowType = workflow.id ? workflow.id.split('_')[0] : BlankNode.name;
		console.log(workflowType);
		if (workflowType === speakWaitingNode.name) {
			let nextStep = workflow.after && workflow.after.id ? workflow.after.id : BlankNode.name;
			if (element.outgoing && element.outgoing[0]) {
				_.forEach(element.outgoing, item => {
					//如果下一步不是空节点，则删除原来的空节点,链接新节点.
					if (nextStep !== BlankNode.name) {
						let targets = this.elementRegistry.filter(elementItem => {
							let taskId = elementItem.businessObject ? elementItem.businessObject.taskId : null;
							if (taskId === nextStep) {
								return elementItem;
							}
						});
						console.log('updateElementConnect targets', targets, this.elementRegistry);
						if (targets && targets.length > 0) {
							if (!this.isConnected(element, targets[0])) {
								this.modeling.connect(element, targets[0]);
							}
						}
						if (item.businessObject.targetRef.taskId.includes(BlankNode.name)) {
							let blankNodeTargets = this.elementRegistry.filter(elementItem => {
								let taskId = elementItem.businessObject ? elementItem.businessObject.taskId : null;
								if (taskId === item.businessObject.targetRef.taskId) {
									return elementItem;
								}
							});
							this.modeling.removeElements(blankNodeTargets);
						}
					}
				});
			}
		}
	}

	/**
	 * 判断2个节点之间是否有关联关系 --连线关系
	 * @param {element} element
	 * @param {element} otherElement
	 */
	isConnected(element, otherElement) {
		console.log('element--otherelement', element, otherElement);
		if (element && element.incoming && element.incoming[0].id && otherElement && otherElement.outgoing && otherElement.outgoing[0].id && element.incoming[0].id === otherElement.outgoing[0].id) {
			return true;
		}
		if (otherElement && otherElement.incoming && otherElement.incoming[0].id && element && element.outgoing && element.outgoing[0].id && otherElement.incoming[0].id === element.outgoing[0].id) {
			return true;
		}
		return false;
	}

	canRender(element) {
		// ignore labels
		return !element.labelTarget;
	}

	drawShape(parentNode, element) {
		// console.log('drawShape : ', element)
		const type = element.type;
		const taskId = element.businessObject.taskId || element.id;
		if (type === 'bpmn:Task') {
			if (taskId.includes(ConfirmInfoNode.name)) {
				return ConfirmInfoNode.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(speakWaitingNode.name)) {
				return speakWaitingNode.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(detectLivingFaceNode.name)) {
				return detectLivingFaceNode.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(messageCollectNode.name)) {
				return messageCollectNode.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(signContractNode.name)) {
				return signContractNode.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(faceCompareNode.name)) {
				return faceCompareNode.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(sendMessageNode.name)) {
				return sendMessageNode.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(requestLocationNode.name)) {
				return requestLocationNode.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(transferNode.name)) {
				return transferNode.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(scanCodeNode.name)) {
				return scanCodeNode.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(hangupNode.name)) {
				return hangupNode.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(BlankNode.name)) {
				return BlankNode.drawShape(parentNode, element);
			} else if (taskId.includes(businessNavigationNode.name)) {
				return businessNavigationNode.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(idCardCompareNode.name)) {
				return idCardCompareNode.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(sendFormNode.name)) {
				return sendFormNode.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(readWaitNode.name)) {
				return readWaitNode.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(sceneTransferNode.name)) {
				return sceneTransferNode.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(summaryConfirmNode.name)) {
				return summaryConfirmNode.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(silentWatingNode.name)) {
				return silentWatingNode.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(optionJudgeNode.name)) {
				return optionJudgeNode.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(messageSummary.name)) {
				return messageSummary.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(sendContentsNode.name)) {
				return sendContentsNode.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(nlpNode.name)) {
				return nlpNode.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(soundReceptionNode.name)) {
				return soundReceptionNode.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(variableJudgeNode.name)) {
				return variableJudgeNode.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(sendDepositionNode.name)) {
				return sendDepositionNode.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(sendSignNode.name)) {
				return sendSignNode.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(variableCollection.name)) {
				return variableCollection.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(inviteHumanThreeParties.name)) {
				return inviteHumanThreeParties.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(collectShortVideo.name)) {
				return collectShortVideo.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(interfaceCall.name)) {
				return interfaceCall.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(startSystemRecordingNode.name)) {
				return startSystemRecordingNode.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(stopSystemRecordingNode.name)) {
				return stopSystemRecordingNode.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(cardCollectionNode.name)) {
				return cardCollectionNode.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			} else if (taskId.includes(llmNode.name)) {
				return llmNode.drawShape(parentNode, element, this.textRenderer, this.eventBus, window.store);
			}
		}
		const shape = this.bpmnRenderer.drawShape(parentNode, element);
		return shape;
	}

	getShapePath(shape) {
		if (
			shape.id.includes(BlankNode.name) ||
			shape.id.includes(ConfirmInfoNode.name) ||
			shape.id.includes(speakWaitingNode.name) ||
			shape.id.includes(detectLivingFaceNode.name) ||
			shape.id.includes(messageCollectNode.name) ||
			shape.id.includes(signContractNode.name) ||
			shape.id.includes(faceCompareNode.name) ||
			shape.id.includes(sendMessageNode.name) ||
			shape.id.includes(requestLocationNode.name) ||
			shape.id.includes(transferNode.name) ||
			shape.id.includes(scanCodeNode.name) ||
			shape.id.includes(hangupNode.name) ||
			shape.id.includes(businessNavigationNode.name) ||
			shape.id.includes(idCardCompareNode.name) ||
			shape.id.includes(sendFormNode.name) ||
			shape.id.includes(readWaitNode.name) ||
			shape.id.includes(sceneTransferNode.name) ||
			shape.id.includes(summaryConfirmNode.name) ||
			shape.id.includes(silentWatingNode.name) ||
			shape.id.includes(optionJudgeNode.name) ||
			shape.id.includes(messageSummary.name) ||
			shape.id.includes(sendContentsNode.name) ||
			shape.id.includes(nlpNode.name) ||
			shape.id.includes(soundReceptionNode.name) ||
			shape.id.includes(variableJudgeNode.name) ||
			shape.id.includes(sendDepositionNode.name) ||
			shape.id.includes(sendSignNode.name) ||
			shape.id.includes(variableCollection.name) ||
			shape.id.includes(inviteHumanThreeParties.name) ||
			shape.id.includes(collectShortVideo.name) ||
			shape.id.includes(interfaceCall.name) ||
			shape.id.includes(cardCollectionNode.name) ||
			shape.id.includes(llmNode.name)
		) {
			return getRoundRectPath(shape, TASK_BORDER_RADIUS);
		}
		return this.bpmnRenderer.getShapePath(shape);
	}
}

CustomRenderer.$inject = ['eventBus', 'bpmnRenderer', 'modeling', 'textRenderer', 'moddle', 'elementRegistry', 'elementFactory', 'injector', 'bpmnFactory', 'styles', 'selection'];
